<?php

namespace app\admin\controller;

use think\Db;
use think\Request;
use think\Validate;
use app\facade\Redis;
use think\Facade\Config as Configs;
use app\models\{User as Users, Region, Second, MenuSecond, Likes, Circle as Circles};

class Circle extends Base
{

    public function circle()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $model = new Circles();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['street'])) {
            $list = $list->where('street', $data['street']);
        } elseif (!empty($data['area'])) {
            $list = $list->where('area', $data['area']);
        } elseif (!empty($data['city'])) {
            $list = $list->where('city', $data['city']);
        } elseif (!empty($data['province'])) {
            $list = $list->where('province', $data['province']);
        }
        if ($admin['type'] == 2) {
            $list = $list->where(['area' => $admin['area']]);
        } elseif ($admin['type'] == 3) {
            $list = $list->where(['street' => $admin['street']]);
        } elseif ($admin['type'] == 4) {
            $list = $list->where(['street' => $admin['street']]);
        }
        if (!empty($data['title'])) {
            $list = $list->where('title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['isshow'])) {
            $list = $list->where('isshow', $data['isshow']);
        }
        $list = $list->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['images'] = json_decode($lists['images'], true);
        }
        return view('', ['list' => $list]);
    }

    //添加修改
    public function circle_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            $data = input('post.');
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            } else {
                $data['images'] = json_encode([], true);
            }
            if (empty($data['street'])) {
                return json(['status' => 1, 'msg' => '街道ID为空，请重新选择街道']);
            }
            $street = (new Region())->where(['id' => $data['street'], 'level' => 4])->find();
            if (empty($data['street'])) {
                return json(['status' => 1, 'msg' => '街道信息不存在，请联系管理员']);
            }
            $area = (new Region())->where(['id' => $street['parent_id'], 'level' => 3])->find();
            if (empty($area)) {
                return json(['status' => 1, 'msg' => '区域信息不存在，请联系管理员']);
            }
            $data['area'] = $area['area_code'];
            $city = (new Region())->where(['id' => $area['parent_id'], 'level' => 2])->find();
            if (empty($city)) {
                return json(['status' => 1, 'msg' => '城市信息不存在，请联系管理员']);
            }
            $data['city'] = $city['area_code'];
            $province = (new Region())->where(['id' => $city['parent_id'], 'level' => 1])->find();
            if (empty($province)) {
                return json(['status' => 1, 'msg' => '省份信息不存在，请联系管理员']);
            }
            $data['province'] = $province['area_code'];
            if ($admin['type'] == 2 && !in_array($data['areaid'], $admin['area'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该区域设置权限']);
            }
            if ($admin['type'] >= 3 && !in_array($data['street'], $admin['street'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该街道设置权限']);
            }
            if (empty($data['id'])) {
                $result = (new Circles())->allowField(true)->save($data);
            } else {
                $result = (new Circles())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Circles())->where('id', $id)->find();
            if ($row) {
                $row['images'] = json_decode($row['images'], true);
            }
            return view('', ['row' => $row]);
        }
    }

    public function second()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        $model = new Second();
        $data = $this->request->get();
        $list = $model->order('id desc');
        if (!empty($data['street'])) {
            $list = $list->where('street', $data['street']);
        } elseif (!empty($data['area'])) {
            $list = $list->where('area', $data['area']);
        } elseif (!empty($data['city'])) {
            $list = $list->where('city', $data['city']);
        } elseif (!empty($data['province'])) {
            $list = $list->where('province', $data['province']);
        }
        if ($admin['type'] == 2) {
            $list = $list->where(['area' => $admin['area']]);
        } elseif ($admin['type'] == 3) {
            $list = $list->where(['street' => $admin['street']]);
        } elseif ($admin['type'] == 4) {
            $list = $list->where(['street' => $admin['street']]);
        }
        if (!empty($data['title'])) {
            $list = $list->where('phone|title', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['isshow'])) {
            $list = $list->where('isshow', $data['isshow']);
        }
        if (!empty($data['type'])) {
            $list = $list->where('type', $data['type']);
        }
        if (!empty($data['status'])) {
            $list = $list->where('status', $data['status']);
        }
        $list = $list->where('isdel', 1)->paginate(10, false, ['query' => $this->request->param()]);
        foreach ($list as $lists) {
            $lists['name'] = (new Users())->where('id', $lists['user_id'])->value('nickname');
            $lists['images'] = json_decode($lists['images'], true);
            $lists['type_name'] = (new MenuSecond())->where('id', $lists['type'])->value('name');
            $lists['province_name'] = (new Region())->where(['area_code' => $lists['province'], 'level' => 1])->value('name');
            $lists['city_name'] = (new Region())->where(['area_code' => $lists['city'], 'level' => 2])->value('name');
            $lists['area_name'] = (new Region())->where(['area_code' => $lists['area'], 'level' => 3])->value('name');
            $lists['street_name'] = (new Region())->where(['area_code' => $lists['street'], 'level' => 4])->value('name');
        }
        $class = (new MenuSecond())->select();
        return view('', ['list' => $list, 'class' => $class]);
    }

    //添加修改
    public function second_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            $data = input('post.');
            if (!empty($data['images'])) {
                $data['images'] = json_encode($data['images'], true);
            }
            if (empty($data['street'])) {
                return json(['status' => 1, 'msg' => '街道ID为空，请重新选择街道']);
            }
            $street = (new Region())->where(['id' => $data['street'], 'level' => 4])->find();
            if (empty($data['street'])) {
                return json(['status' => 1, 'msg' => '街道信息不存在，请联系管理员']);
            }
            $area = (new Region())->where(['id' => $street['parent_id'], 'level' => 3])->find();
            if (empty($area)) {
                return json(['status' => 1, 'msg' => '区域信息不存在，请联系管理员']);
            }
            $data['area'] = $area['area_code'];
            $city = (new Region())->where(['id' => $area['parent_id'], 'level' => 2])->find();
            if (empty($city)) {
                return json(['status' => 1, 'msg' => '城市信息不存在，请联系管理员']);
            }
            $data['city'] = $city['area_code'];
            $province = (new Region())->where(['id' => $city['parent_id'], 'level' => 1])->find();
            if (empty($province)) {
                return json(['status' => 1, 'msg' => '省份信息不存在，请联系管理员']);
            }
            $data['province'] = $province['area_code'];
            if ($admin['type'] == 2 && !in_array($data['areaid'], $admin['area'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该区域设置权限']);
            }
            if ($admin['type'] >= 3 && !in_array($data['street'], $admin['street'])) {
                return json(['status' => 1, 'data' => [], 'msg' => '无该街道设置权限']);
            }
            if (empty($data['id'])) {
                $result = (new Second())->allowField(true)->save($data);
            } else {
                $result = (new Second())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new Second())->where('id', $id)->find();
            if ($row) {
                $row['province_name'] = (new Region())->where(['area_code' => $row['province'], 'level' => 1])->value('name');
                $row['city_name'] = (new Region())->where(['area_code' => $row['city'], 'level' => 2])->value('name');
                $row['area_name'] = (new Region())->where(['area_code' => $row['area'], 'level' => 3])->value('name');
                $row['street_name'] = (new Region())->where(['id' => $row['street'], 'level' => 4])->value('name');
                $row['images'] = json_decode($row['images'], true);
            }
            $class = (new MenuSecond())->where('ishsow', 1)->select();
            return view('', ['row' => $row, 'class' => $class]);
        }
    }

    public function menu_second()
    {
        $ModelFun = new MenuSecond();
        $data = $this->request->get();
        $list = $ModelFun->order('sort desc')->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    //添加修改
    public function menu_second_add()
    {
        if ($this->request->isPost()) {
            $rdslist = Redis::get('clickreturn');
            if ($rdslist) {
                return json(['status' => 1, 'data' => [], 'msg' => '请' . $rdslist . 's后再试']);
            }
            Redis::setex('clickreturn', 2, 1);
            $admin = $this->request->admin;
            $admin['area'] = explode(',', $admin['area']);
            $admin['street'] = explode(',', $admin['street']);
            if ($admin['type'] != 1) {
                return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
            }
            $data = input('post.');
            if (empty($data['id'])) {
                $result = (new MenuSecond())->allowField(true)->save($data);
            } else {
                $result = (new MenuSecond())->allowField(true)->save($data, ['id' => $data['id']]);
            }
            if ($result) {
                return json(['status' => 0, 'msg' => '操作成功']);
            } else {
                return json(['status' => 1, 'msg' => '操作失败']);
            }
        } else {
            $id = $this->request->get('id');
            $row = (new MenuSecond())->where('id', $id)->find();
            return view('', ['row' => $row]);
        }
    }

    public function menu_second_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        if ($admin['type'] != 1) {
            return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        $result = (new MenuSecond())->where('id', $id)->delete();
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }


    public function second_del()
    {
        $admin = $this->request->admin;
        $admin['area'] = explode(',', $admin['area']);
        $admin['street'] = explode(',', $admin['street']);
        if ($admin['type'] != 1) {
            return json(['status' => 1, 'data' => [], 'msg' => '无操作权限']);
        }
        $id = trim(input('id'), ',');
        $result = (new Second())->where('id', $id)->update(['isdel' => 2]);
        if ($result) {
            return json(['status' => 0, 'msg' => '操作成功']);
        } else {
            return json(['status' => 1, 'msg' => '删除失败']);
        }
    }
}
